import { useEffect, useState } from 'react'
import styles from './index.module.less'
import { Button, Image, Modal, SafeArea, Toast } from 'antd-mobile'
import { history } from 'umi'
import { postData, deleteData, getData } from '@/servers/http'
import { api } from '@/servers/api'
import { getCookie } from '@/storage'
import wx from 'weixin-webview-jssdk'

type DetailType = {
    entry_time: string
    company_name: string
    credit_code: string
    company_type: string
    region_name: string
    parent_industry_name: string
    industry_name: string
    profession_name: string
    salary: string
    attachment_file: { url: string; name: string }[]
    origin_type: number
}

const TrackView = () => {
    const { id, group_id, domain } = history.location.query || {}
    const [detail, setDetail] = useState<DetailType>()
    const [unitObj, setUnitObj] = useState<Record<any, string>>({})

    /** 获取详情 */
    const getDetail = () => {
        if (!Number(id)) return
        const params = { access_token: getCookie('MLHTOKEN'), domain }
        postData(`${api.employDetail}/${id}`, params).then(({ data }) => {
            setDetail(data)
        })
    }
    /** 获取单位性质对象 */
    const getUnitObj = () => {
        getData(api.companyType).then(({ data }) => {
            setUnitObj(data || {})
        })
    }
    useEffect(() => {
        document.title = '查看'
        getDetail()
        getUnitObj()
    }, [])
    /** 重新录入 */
    const reEntry = () => {
        history.push(`/track/input?id=${id}&group_id=${group_id}&isRe=1&domain=${domain}`)
    }
    /** 删除 */
    const deleteDetail = () => {
        Modal.confirm({
            content: '确定要删除吗，该操作不可逆。',
            confirmText: '确定',
            onConfirm: async () => {
                const params = { access_token: getCookie('MLHTOKEN'), domain }
                await deleteData(`${api.submitEmploys}/${id}`, params)
                Toast.show({
                    icon: 'success',
                    content: '删除成功',
                })
                wx.miniProgram.navigateBack()
            },
        })
    }
    /** 去编辑 */
    const toEdit = () => {
        history.push(`/track/input?id=${id}&group_id=${group_id}&domain=${domain}`)
    }

    return (
        <div className={styles.track_view}>
            <div className={styles.content_box}>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>入职时间</label>
                        <p className={styles.text}>{detail?.entry_time}</p>
                    </div>
                </div>
                <div className={styles.line} />
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>单位名称</label>
                        <p className={styles.text}>{detail?.company_name}</p>
                    </div>
                </div>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>单位性质</label>
                        <p className={styles.text}>{unitObj[detail?.company_type ?? 0]}</p>
                    </div>
                </div>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>统一信用代码</label>
                        <p className={styles.text}>{detail?.credit_code}</p>
                    </div>
                </div>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>所在地</label>
                        <p className={styles.text}>{detail?.region_name}</p>
                    </div>
                </div>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>行业</label>
                        <p className={styles.text}>
                            {detail?.parent_industry_name}/{detail?.industry_name}
                        </p>
                    </div>
                </div>
                <div className={styles.line} />
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>岗位名称</label>
                        <p className={styles.text}>{detail?.profession_name}</p>
                    </div>
                </div>
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>月薪</label>
                        <p className={styles.text}>{detail?.salary}元</p>
                    </div>
                </div>
                <div className={styles.line} />
                <div className={[styles.item_box, styles.file_box].join(' ')}>
                    <label>附件材料</label>
                    <div className={styles.img_box}>
                        {detail?.attachment_file?.map(item => {
                            return <Image className={styles.img} key={item.url} src={item.url} />
                        })}
                    </div>
                </div>
                <div className={styles.line} />
                <div className={styles.item_box}>
                    <div className={styles.item_wrap}>
                        <label>记录来源</label>
                        <p className={styles.text}>{detail?.origin_type === 1 ? '机构录入' : '学员录入'}</p>
                    </div>
                </div>
            </div>
            <SafeArea position="bottom" />
            <div className={styles.footer_box}>
                <div className={styles.space_box}>
                    {detail?.origin_type === 1 ? (
                        <Button className={styles.btn} onClick={reEntry}>
                            重新录入
                        </Button>
                    ) : (
                        <>
                            <Button className={styles.btn} onClick={deleteDetail}>
                                删除
                            </Button>
                            <Button className={styles.btn} onClick={toEdit}>
                                编辑
                            </Button>
                        </>
                    )}
                </div>
                <SafeArea position="bottom" />
            </div>
        </div>
    )
}

export default TrackView
